package com.media.themovieworld.activity;


import java.net.SocketTimeoutException;
import java.util.concurrent.TimeoutException;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;

import com.google.android.gcm.GCMRegistrar;
import com.media.themovieworld.Config;
import com.media.themovieworld.api.ApiParams;
import com.media.themovieworld.api.ApiRequest;
import com.media.themovieworld.api.RequestModel;
import com.media.themovieworld.common.fragment.MovieTaskScreen.MovieTaskListener;
import com.media.themovieworld.model.RegisterResultModel;
import com.media.themovieworld.util.network.HttpMovieTask;
import com.media.themovieworld.util.network.util.JsonUtil;


public class MovieWorldConfigActivity extends BaseFragmentActivity{

	String regId;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		if (this instanceof MainActivity) {
			registerGCM(Config.SENDER_ID);
		}
	}
	
	protected void registerGCM(String senderId) {
		GCMRegistrar.checkDevice(this);
		GCMRegistrar.checkManifest(this);
		registerReceiver(mHandleMessageReceiver, new IntentFilter(
				Config.DISPLAY_MESSAGE_ACTION));
		regId = GCMRegistrar.getRegistrationId(this);
		Log.d("GCM REGID == ",""+regId);
		if (regId == null || regId.equals("")) {
			GCMRegistrar.register(this, senderId);
		}
		if (regId.equals("")) {
			// Automatically registers application on startup.
			GCMRegistrar.register(this, Config.SENDER_ID);
		} else {
			registDevice(regId);
			// Device is already registered on GCM, check server.
			if (GCMRegistrar.isRegisteredOnServer(this)) {
				// Skips registration.
				// mDisplay.append(getString(R.string.already_registered) +
				// "\n");
			} else {
				// Try to register again, but not in the UI thread.
				// It's also necessary to cancel the thread onDestroy(),
				// hence the use of AsyncTask instead of a raw thread.
				final Context context = this;
//			    registDevice(regId);
			}
		}
	}
	
	private final BroadcastReceiver mHandleMessageReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			String newMessage = intent.getExtras().getString(Config.EXTRA_MESSAGE);			
//			WakeLocker.acquire(getApplicationContext());
//			WakeLocker.release();
		}
	};
	
	public void registDevice(String regId){
		final RequestModel requestData = ApiParams.getdeviceConnectParams(this, regId);
		executeSoapTask(requestData, true, RegisterResultModel.class, new MovieTaskListener() {
			
			@Override
			public void onTaskComplete(Object object) {
				finishRegisterDevices(object);
			}
		});
	}
	
	public void finishRegisterDevices(Object object){
		
	}
	
	/**
	 * Execute soap task.
	 *
	 * @param <T> the generic type
	 * @param soapModel the soap model
	 * @param isShowProgresss the is show progresss
	 * @param listener the listener
	 * @param returnType the return type
	 */
	public <T> void executeSoapTask(final RequestModel soapModel,boolean isShowProgress,final Class<T> returnType,final MovieTaskListener listener){
		HttpMovieTask<String,T> httpSoapTask = new HttpMovieTask<String, T>() {

			@Override
			public T processInBackground(String... pParams)
					throws SocketTimeoutException, TimeoutException {
				String responds = null;
				try {
					responds = ApiRequest.sendRequest(soapModel.params,soapModel.api);
				} catch (Throwable e) {
					e.printStackTrace();
					return null;
				}
				
				return JsonUtil.convertObjectFromJsonString(responds, returnType);
			}

			@Override
			public void onUIUpDate(T result) {
//				listener.onTaskComplete(null);
				if (this !=null && listener!=null) {
					listener.onTaskComplete(result);
				}
			}
			
		};
		httpSoapTask.run(this, null ,isShowProgress, soapModel.api);
	}
	
	@Override
	protected void onResume() {
		Log.d("GCM REGID == ",""+regId);
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if (this instanceof MainActivity) {
			unregisterReceiver(mHandleMessageReceiver);
			GCMRegistrar.unregister(this);
			GCMRegistrar.onDestroy(this);
		}
	}
}
